import { Controller, Get, Post, Body, Param, HttpException, HttpStatus } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Public } from 'src/core/decorators/public.decorator'
import { ApiTags, ApiOperation } from '@nestjs/swagger';

@ApiTags('用户')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) { }

  @ApiOperation({ summary: '创建用户' })
  @Public()
  @Post('/create')
  createUser(@Body() createUserDto: CreateUserDto) {
    return this.userService.register(createUserDto);
  }

  @ApiOperation({ summary: '获取用户列表' })
  @Get('/getAllUserList')
  findAll() {
    return this.userService.findAll();
  }



  @Post('/updateUser')
  update(@Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(updateUserDto);
  }

  @Get('/deleteUser/:id')
  remove(@Param('id') id: string) {
    return this.userService.remove(id);
  }

  @Post('/setUserState')
  banUser(@Body() data: UpdateUserDto) {
    // 设置键的过期时间为0秒（EXPIRE key 0 或 PEXPIRE key 0）：
    // 这意味着立即使键过期，即该键会立刻从数据库中被删除
    if (data.time == null || data.time == undefined
      || data.type == null || data.type == undefined) {
      throw new HttpException('缺少参数', HttpStatus.BAD_REQUEST)
    }
    if (data.time == 0) throw new HttpException('封禁时间不能为0', HttpStatus.BAD_REQUEST)
    return this.userService.banOrOpenUser(data);
  }
}
